package com.suda.sudaexamsystem.service.impl;

import com.suda.sudaexamsystem.mapper.RequestMapper;
import com.suda.sudaexamsystem.mapper.UserClazzMapper;
import com.suda.sudaexamsystem.pojo.User;
import com.suda.sudaexamsystem.service.RequestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class RequestServiceImpl implements RequestService {
    @Autowired
    private RequestMapper requestMapper;
    @Autowired
    private UserClazzMapper userClazzMapper;

    @Override
    @Transactional
    public void sendApplyRequest(int userId, int clazzId) {
        // 判断用户是否已经在班级中
        if (userClazzMapper.isUserInClazz(userId, clazzId)) {
            log.info("用户已经在班级中了 userId：" + userId);
            return;
        }
        requestMapper.sendApplyRequest(userId, clazzId);
    }

    @Override
    @Transactional
    public void deleteApplyRequest(int userId, int clazzId) {
        requestMapper.deleteApplyRequest(userId, clazzId);
    }

    @Override
    @Transactional
    public void deleteAllForClazz(int clazzId) {
        requestMapper.deleteAllForClazz(clazzId);
    }

    @Override
    @Transactional
    public void deleteBatchRequest(String userIds, int clazzId) {
        requestMapper.deleteBatchRequest(userIds, clazzId);
    }

    @Override
    @Transactional
    public List<User> findByClazzId(int clazzId) {
        return requestMapper.findByClazzId(clazzId);
    }
}
